package com.tairan.chapter.flowable.controller;

import com.google.common.collect.Maps;
import com.tairan.chapter.flowable.service.ReProcDefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Map;

@RestController
@RequestMapping("/act/process")
public class ReProcDefController {

    @Autowired
    private ReProcDefService reProcDefService;

    @RequestMapping("/resource/read")
    public void resourceRead(String procDefId,
                             String proInsId,
                             String resType,
                             HttpServletResponse response) throws Exception {
        InputStream inputStream = reProcDefService.resourceRead(procDefId, proInsId, resType);
        byte[] b = new byte[1024];
        int len = -1;
        while ((len = inputStream.read(b, 0, 1024)) != -1) {
            response.getOutputStream().write(b, 0, len);
        }
        response.getOutputStream().flush();
    }

    @RequestMapping("/start")
    public String startProcess(String procDefKey) {
        Map<String, Object> vars = Maps.newHashMap();
        String processId = reProcDefService.startProcess(procDefKey, "test", "1", vars);
        return processId + "——启动流程成功";
    }
}
